﻿Imports System.Data.OleDb

Public Class detail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim sql As String = "INSERT INTO Comment (Author,Title,Comment,cricfkid) VALUES (@f1,@f2,@f3,@f4)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("maniaConnectionstring").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", T1.Text)
        cmd.Parameters.AddWithValue("@f3", T2.Text)
        cmd.Parameters.AddWithValue("@f4", Request.QueryString("ID"))


        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("Detail.aspx?ID=" & Request.QueryString("ID"))

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Dim sql As String = "INSERT INTO crictags (cricmaniafkid,TagfkID) VALUES (@f1,@f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("maniaConnectionstring").ConnectionString)
        conn.Open()
        For Each cb As ListItem In Cbl.Items
            If cb.Selected = True Then
                'add this to the database
                Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
        Dim sql As String = "INSERT INTO Tags (TagName) VALUES (@f1)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("maniaConnectionstring").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", At.Text)
        cmd.ExecuteNonQuery()

        Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM Tags ORDER BY ID Desc", conn)
        Dim dr As OleDbDataReader = cmd2.ExecuteReader()
        dr.Read()
        Dim highestId = dr(0)

        Dim associate As String = "INSERT INTO crictags (cricmaniafkid,TagfkID) VALUES (@f1,@f2)"
        Dim cmd3 As OleDbCommand = New OleDbCommand(associate, conn)
        cmd3.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
        cmd3.Parameters.AddWithValue("@f2", highestId)
        cmd3.ExecuteNonQuery()

    End Sub
End Class